package com.fg.util;

import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Properties;
import java.util.TimeZone;


public class UsefulFunctions {
	
	/**
	 * for reduce text length
	 */
	public static String textOverFlow(String str, int count){
		
		if(str.length() > count){
			str = ((new StringBuffer(str)).substring(0, count-3));
			str = str+"...";
		}
		
		return str;	
	}
	

	/**
	 * 
	 * @param str yyyy-MM-dd 00:00:00
	 * @return
	 * @throws Exception
	 */
	public static long getD_day(String str) throws Exception{
		int d_day = 0;
		SimpleDateFormat dateFormatUTC = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dateFormatUTC.setTimeZone(TimeZone.getTimeZone("Asia/Seoul"));	//DB타임존 맞추기

		long interval = dateFormatUTC.parse(str).getTime() - new Date().getTime();
		
		long min = 1000 * 60;
		long hour = min * 60;
		long day = hour * 24;
		long result = (interval / day)+1;
		return  result;
	}
	
	
	/**
	 * Date Optimization 
	 */
	
	
	public static long parseDateFormat(String str) throws Exception{		
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = format.parse(str);
		return date.getTime();
	}
	
	public static String parseOnlyDateTimeFormat(String str) throws Exception{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = format.parse(str);
		
		DateFormat new_format = new SimpleDateFormat("MM월 dd일  HH시 mm분");
		str = new_format.format(date);
		return str;
	}
	public static String parseOnlyDateFormat(String str) throws Exception{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = format.parse(str);

		DateFormat new_format = new SimpleDateFormat("MM월 dd일");
		str = new_format.format(date);
		return str;
	}
	
	/**
	 * 
	 * @param subDomainName : ?�브 ?�메??�?	 * @return ???�브 ?�메??url
	 * @throws Exception
	 */
	public static String getSubDomain(String subDomainName) {
		try {
			String propertiesName = "properties/subDomain.properties";
			InputStream in = UsefulFunctions.class.getClassLoader().getResourceAsStream(propertiesName);
			Properties properties = new Properties();
			properties.load(in);
			return properties.getProperty(subDomainName);
		} catch(IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Properties 
	 * @param propertiesName (ex. "properties/FacebookAPI.properties")
	 * @param key (ex. "APIKey")
	 * @return Value
	 * @throws Exception
	 */
	public static String getPropertiesValue(String propertiesName, String key) throws Exception {
		InputStream in = UsefulFunctions.class.getClassLoader().getResourceAsStream(propertiesName);
		Properties properties = new Properties();
		properties.load(in);
		return properties.getProperty(key);
	}
}
